import React, { useEffect, useState } from "react";
export const TestClosure = () => {
  const [num, setNum] = useState(0);
  const add = () => setNum(num + 1);
  useEffect(() => {
    const id = setInterval(() => {
      console.log("number in setInterval", num);
    }, 1000);
    return () => clearInterval(id);
  }, [num]);
  useEffect(() => {
    return () => {
      console.log(num);
    };
  }, [num]);
  return (
    <div>
      <button onClick={add}>add</button>
      <p>卸载值:{num}</p>
    </div>
  );
};
